package com.xw.service.impl;

import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.IdcardUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xw.entity.Contact;
import com.xw.enums.ResultEnum;
import com.xw.mapper.ContactMapper;
import com.xw.service.IContactService;
import com.xw.utils.R;
import com.xw.vo.ResultVO;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author y
 * @since 2021-09-15
 */
@Service
public class ContactServiceImpl extends ServiceImpl<ContactMapper, Contact> implements IContactService {

    @Resource
    private ContactMapper contactMapper;

    @Override
    public ResultVO addContact(Contact contact) {
        System.out.println(contact);

        if (contact == null || contact.getOpenid() == null) {
            return R.fail();
        }

        if ("身份证".equals(contact.getCertificateType())) {
            if (contact.getIdcard().length() == 15) {
                String convert15To18 = IdcardUtil.convert15To18(contact.getIdcard());
                boolean validCard = IdcardUtil.isValidCard(convert15To18);
                if (!validCard) {
                    return R.fail(ResultEnum.VALID_CARD_FAIL);
                }
            } else {
                boolean validCard = IdcardUtil.isValidCard(contact.getIdcard());
                if (!validCard) {
                    return R.fail(ResultEnum.VALID_CARD_FAIL);

                }
            }
        } else if ("护照".equals(contact.getCertificateType())) {

            if (contact.getIdcard().length() != 9) {
                return R.fail(ResultEnum.PASSPORT_FORMAT);
            }

        }

        int insert = contactMapper.insert(contact);

        return insert > 0 ? R.success() : R.fail();
    }

    @Override
    public ResultVO findContact(String recordId) {
        Contact contact = contactMapper.selectById(recordId);
        contact.setOpenid(null);
        return R.success(contact);
    }

    @Override
    public ResultVO delContact(String recordId, String openId) {

        LambdaQueryWrapper<Contact> lambdaQueryWrapper = new LambdaQueryWrapper<Contact>();
        lambdaQueryWrapper.eq(Contact::getOpenid, openId);
        lambdaQueryWrapper.eq(Contact::getId, recordId);

        List<Contact> contacts = contactMapper.selectList(lambdaQueryWrapper);
        if (contacts.size() > 0) {
            List<Integer> collect = contacts.stream().map(Contact::getId).collect(Collectors.toList());
            contactMapper.deleteBatchIds(collect);
        }
        return R.success();
    }

    @Override
    public ResultVO updContact(Contact contact) {

        if (ObjectUtils.isEmpty(contact) || ObjectUtils.isEmpty(contact.getId())) {
            return R.fail();
        }

        contactMapper.updateById(contact);

        return R.success();
    }

    @Override
    public ResultVO findContactList(String openId) {
        LambdaQueryWrapper<Contact> lambdaQueryWrapper = new LambdaQueryWrapper<Contact>();
        lambdaQueryWrapper.eq(Contact::getOpenid, openId);
        List<Contact> contacts = contactMapper.selectList(lambdaQueryWrapper);
        contacts.forEach(item -> {
            item.setOpenid(null);
            item.setDesIdcard(DesensitizedUtil.idCardNum(item.getIdcard(), 4, 4));
        });
        return R.success(contacts);
    }
}
